package com.chart.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chart.chart.domian.vo.ChartDetailVO;
import com.chart.chart.domian.vo.ChartRecordVO;
import com.chart.chart.domian.vo.RelatedChartVO;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.core.domain.entity.ChartList;
import com.chart.common.core.domain.entity.ChartRecord;
import com.chart.common.utils.SecurityUtils;
import com.chart.system.mapper.ChartListMapper;
import com.chart.system.mapper.ChartRecordMapper;
import com.chart.system.service.IChartRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ChartRecordServiceImpl  extends ServiceImpl<ChartRecordMapper, ChartRecord> implements IChartRecordService {



    @Autowired
    private ChartRecordMapper chartRecordMapper;

    @Autowired
    private ChartListMapper chartListMapper;

//    @Override
//    public ChartRecordVO getRecordById(Long id) {
//
//        ChartRecord chartRecord = chartRecordMapper.selectById(id);
//
//        ChartRecordVO chartRecordVO = new ChartRecordVO();
//        BeanUtils.copyProperties(chartRecord,chartRecordVO);
//        Integer chartCode = chartRecord.getChartCode();
//
//        LambdaQueryWrapper<ChartList> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(ChartList::getChartCode,chartCode);
//        List<ChartList> chartLists = chartListMapper.selectList(wrapper);
//        ChartList chartList = chartLists.get(0);
//        Integer relateSort = chartList.getRelateSort();
//
//
//        LambdaQueryWrapper<ChartList> wrappers = new LambdaQueryWrapper<>();
//        wrappers.eq(ChartList::getRelateSort,relateSort);
//        wrappers.eq(ChartList::getStatus,1);
//
//        List<ChartList> chartLists1 = chartListMapper.selectList(wrappers);
//
//
//        // 构建与图表相关的图表列表
//        List<RelatedChartVO> relateList = new ArrayList<>();
//        for (ChartList relatedChart : chartLists1) {
//            RelatedChartVO relatedChartVO = new RelatedChartVO();
//            relatedChartVO.setChartCode(relatedChart.getChartCode());
//            relatedChartVO.setChartName(relatedChart.getChartName());
//            relatedChartVO.setChartPath(relatedChart.getChartPath());
//            relatedChartVO.setChartConfigUi(relatedChart.getChartConfigUi());
//            relatedChartVO.setChartMapName(relatedChart.getChartMapName());
//            relatedChartVO.setChartMapPath(relatedChart.getChartMapPath());
//            relatedChartVO.setIsComplex(relatedChart.getIsComplex());
//            relateList.add(relatedChartVO);
//        }
//
//
//        // 将相关图表列表设置到 ChartRecordVO 中
//        chartRecordVO.setRelateList(relateList);
//
//
//        return chartRecordVO;
//    }


//未判断 relate_sort == 0 只返回本身

//    @Override
//    public AjaxResult getRecordById(Long id) {
//        ChartRecord chartRecord = chartRecordMapper.selectById(id);
//        // 校验 ID 是否为空或无效
//        if (chartRecord == null || id <= 0) {
//            String errorMsg = String.format("无效的 ID 参数: %d", id);
//            log.warn(errorMsg);
//            return AjaxResult.error(404,errorMsg);
//        }
//
//        ChartRecordVO chartRecordVO = new ChartRecordVO();
//        BeanUtils.copyProperties(chartRecord, chartRecordVO);
//        Integer chartCode = chartRecord.getChartCode();
//
//        LambdaQueryWrapper<ChartList> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(ChartList::getChartCode, chartCode);
//        List<ChartList> chartLists = chartListMapper.selectList(wrapper);
//
//        if (chartLists.isEmpty()) {
//            log.warn("未找到与 ChartCode " + chartCode + " 相关的 ChartList");
//            return AjaxResult.error(404, "未找到相关 ChartList");
//        }
//
//        ChartList chartList = chartLists.get(0);
//        Integer relateSort = chartList.getRelateSort();
//
//        LambdaQueryWrapper<ChartList> wrappers = new LambdaQueryWrapper<>();
//        wrappers.eq(ChartList::getRelateSort, relateSort);
//        wrappers.eq(ChartList::getStatus, 1);
//
//        List<ChartList> chartLists1 = chartListMapper.selectList(wrappers);
//
//        if (chartLists1.isEmpty()) {
//            log.warn("未找到关联排序为 " + relateSort + " 的 ChartList");
//            return AjaxResult.error(404, "未找到符合条件的关联记录");
//        }
//
//
//        // 构建与图表相关的图表列表
//        List<RelatedChartVO> relateList = new ArrayList<>();
//        RelatedChartVO matchedChartVO = null;
//
//        for (ChartList relatedChart : chartLists1) {
//            RelatedChartVO relatedChartVO = new RelatedChartVO();
//            relatedChartVO.setChartCode(relatedChart.getChartCode());
//            relatedChartVO.setChartName(relatedChart.getChartName());
//            relatedChartVO.setChartPath(relatedChart.getChartPath());
//            relatedChartVO.setChartConfigUi(relatedChart.getChartConfigUi());
//            relatedChartVO.setChartMapName(relatedChart.getChartMapName());
//            relatedChartVO.setChartMapPath(relatedChart.getChartMapPath());
//            relatedChartVO.setIsComplex(relatedChart.getIsComplex());
//
//            if (relatedChart.getChartCode().equals(chartCode)) {
//                // 如果找到与 chartCode 相等的图表，单独记录
//                matchedChartVO = relatedChartVO;
//            } else {
//                relateList.add(relatedChartVO);
//            }
//        }
//
//        // 将与 chartCode 相等的记录放在第一位
//        if (matchedChartVO != null) {
//            relateList.add(0, matchedChartVO);
//        }
//
//        // 将相关图表列表设置到 ChartRecordVO 中
//        chartRecordVO.setRelateList(relateList);
//
//        return  AjaxResult.success(chartRecordVO);
//    }



    /**
     * 如果 relate_sort 为 0，则 relateList 只包含图表本身
     * @param id
     * @return
     */

    @Override
    public AjaxResult getRecordById(Long id) {
        ChartRecord chartRecord = chartRecordMapper.selectById(id);
        // 校验 ID 是否为空或无效
        if (chartRecord == null || id <= 0) {
            String errorMsg = String.format("无效的 ID 参数: %d", id);
            log.warn(errorMsg);
            return AjaxResult.error(404, errorMsg);
        }

        ChartRecordVO chartRecordVO = new ChartRecordVO();
        BeanUtils.copyProperties(chartRecord, chartRecordVO);
        Integer chartCode = chartRecord.getChartCode();

        LambdaQueryWrapper<ChartList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChartList::getChartCode, chartCode);
        List<ChartList> chartLists = chartListMapper.selectList(wrapper);

        if (chartLists.isEmpty()) {
            log.warn("未找到与 ChartCode " + chartCode + " 相关的 ChartList");
            return AjaxResult.error(404, "未找到相关 ChartList");
        }

        ChartList chartList = chartLists.get(0);
        Integer relateSort = chartList.getRelateSort();

        // 如果 relate_sort 为 0，直接返回当前图表信息
        if (relateSort == 0) {
            RelatedChartVO relatedChartVO = new RelatedChartVO();
            relatedChartVO.setChartCode(chartList.getChartCode());
            relatedChartVO.setChartName(chartList.getChartName());
            relatedChartVO.setChartPath(chartList.getChartPath());
            relatedChartVO.setChartConfigUi(chartList.getChartConfigUi());
            relatedChartVO.setChartMapName(chartList.getChartMapName());
            relatedChartVO.setChartMapPath(chartList.getChartMapPath());
            relatedChartVO.setIsComplex(chartList.getIsComplex());

            List<RelatedChartVO> relateList = new ArrayList<>();
            relateList.add(relatedChartVO);
            chartRecordVO.setRelateList(relateList);

            return AjaxResult.success(chartRecordVO);
        }

        LambdaQueryWrapper<ChartList> wrappers = new LambdaQueryWrapper<>();
        wrappers.eq(ChartList::getRelateSort, relateSort);
        wrappers.eq(ChartList::getStatus, 1);

        List<ChartList> chartLists1 = chartListMapper.selectList(wrappers);

        if (chartLists1.isEmpty()) {
            log.warn("未找到关联排序为 " + relateSort + " 的 ChartList");
            return AjaxResult.error(404, "未找到符合条件的关联记录");
        }

        // 构建与图表相关的图表列表
        List<RelatedChartVO> relateList = new ArrayList<>();
        RelatedChartVO matchedChartVO = null;

        for (ChartList relatedChart : chartLists1) {
            RelatedChartVO relatedChartVO = new RelatedChartVO();
            relatedChartVO.setChartCode(relatedChart.getChartCode());
            relatedChartVO.setChartName(relatedChart.getChartName());
            relatedChartVO.setChartPath(relatedChart.getChartPath());
            relatedChartVO.setChartConfigUi(relatedChart.getChartConfigUi());
            relatedChartVO.setChartMapName(relatedChart.getChartMapName());
            relatedChartVO.setChartMapPath(relatedChart.getChartMapPath());
            relatedChartVO.setIsComplex(relatedChart.getIsComplex());

            if (relatedChart.getChartCode().equals(chartCode)) {
                // 如果找到与 chartCode 相等的图表，单独记录
                matchedChartVO = relatedChartVO;
            } else {
                relateList.add(relatedChartVO);
            }
        }

        // 将与 chartCode 相等的记录放在第一位
        if (matchedChartVO != null) {
            relateList.add(0, matchedChartVO);
        }

        // 将相关图表列表设置到 ChartRecordVO 中
        chartRecordVO.setRelateList(relateList);

        return AjaxResult.success(chartRecordVO);
    }

}










